using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AandEDLL;
using AandEDLL.DataAccess;

namespace ASA
{
    public partial class ChangeUserInfoDialog : ASA.BaseDialog
    {
        public User user;

        public ChangeUserInfoDialog(User user)
        {
            InitializeComponent();
            this.user = user;
        }

        private void ChangeUserInfoDialog_Load(object sender, EventArgs e)
        {
            tbUserName.Text = user.UserName;
            //tbPassword.Text = user.Password;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (user.Password != tbOPassword.Text)
                {
                    MessageBox.Show("Old password is not correct!");
                    return;
                }                
                if (tbPassword.Text.Trim().Length == 0)
                {
                    MessageBox.Show(this, "Password field cannot be empty!","Password empty",MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                }
                else if (!tbPassword.Text.Trim().Equals(tbRePassword.Text.Trim()))
                {
                    MessageBox.Show("Password didn't match reenter password");
                    return;
                }
                user.UserName = tbUserName.Text.Trim();
                user.Password = tbPassword.Text.Trim();
                user.LastLoginDate = DateTime.Now;
                Accessor.UpdateUser(user);
            }
            catch (Exception)
            {
                MessageBox.Show("Error saving changed user information.");
            }

            this.DialogResult = DialogResult.OK;
        }      
    }
}

